/**
 * Submit a form in javascript
 * @param form
 */
function submitForm(form) {
	$("#" + form).submit();
}


/**
 * return the code of the pressed key
 * @param event
 * @returns
 */
function getKeyPressed(event) {
	return  (event.which) ? event.which : event.keyCode
			
}

/**
 * Set the focus on the next item
 * @param event
 * @param next
 */
function setFocusNext(event,next) {
	if (isReturn(event)) {
		$("#"+next).focus();
	}
}

/**
 * This function take a message ([balise1]message[/balise1][balise2]message2[/balise2])
 * and returns the message that is within the first occurence of the balise 'balise'
 * @param balise
 * @param message
 * @returns
 */
function getMessage(balise,message) {
	var deb = message.indexOf("["+balise+"]")+balise.length+2;
	var fin = message.indexOf("[/"+balise+"]");
	return message.substring(deb,fin);
}


/**
 * The function removes the error of an element
 * @param element
 */
function removeError(element) {
	if ($(element).hasClass('error')) {
		$(element).removeClass('error');
	}
}

/**
 * This function computes the params which are in an element which id is form
 * @param form
 * @returns {String}
 */
function computeData(form) {
	var request = "";
	$("#"+form+" input ").each(function() {
		if ($(this).attr('name')!='') {
			if (request!='')
				request+='&';
			request+=$(this).attr('name')+"="+$(this).attr('value');
		}
	});
	$("#"+form+" select ").each(function() {
		if ($(this).attr('name')!='') {
			if (request!='')
				request+='&';
			request+=$(this).attr('name')+"="+$(this).attr('value');
		}
	});
	return request;
}

/**
 * This function returns the action of a form
 * @param form
 * @returns
 */
function computeAction(form) {
	return $('#'+form).attr('action');
}

/**
 * This function returns true if the press key was enter. false otherwise
 * @param event
 * @returns {Boolean}
 */
function isReturn(event) {
	return getKeyPressed(event)==13;
}

/**
 * This function submit a form using ajax
 * @param url
 * @param data
 * @param method
 * @param success
 */
function submitFormAjax(url,data,method,success) {
	$.ajax({
		  url: url,
		  data:data,
		  type:method,
		  context: document.body,
		  error:function() {
			  alert('An error has occured... Sorry for the disagreement...');
		  },
		  success:success
		});
}
/**
 * This function return the number of days in a month
 * @param month
 * @param year
 * @returns
 */
function daysInMonth(month,year) {
    return new Date(year, month, 0).getDate();
}

/**
 * This function change the background image of a button
 * @param button
 * @param part
 * @param img
 */
function setButtonPressed(button,part,img) {
	$("#"+button+" "+part).css('background-image','url('+img+')');
}

function getInteger(i) {
	var result=0;
	for (result = 0;result<i;result++) {}
	return result;
}

function pressPlusOneButton(button,action) {
	setButtonPressed(button,'span','/resources/img/main/button_newevent_left_pressed.png');
	setButtonPressed(button,'span span','/resources/img/main/button_newevent_right_pressed.png');
	setButtonPressed(button,'span span span','/resources/img/main/button_newevent_fill_pressed.png');
	setTimeout(action+
			"setButtonPressed('"+button+"','span','/resources/img/main/button_newevent_left.png');"+
			"setButtonPressed('"+button+"','span span','/resources/img/main/button_newevent_right.png');"+
			"setButtonPressed('"+button+"','span span span','/resources/img/main/button_newevent_fill.png');",100);
}
/**
 * Return the max of two values
 * @param a
 * @param b
 * @returns
 */
function getMax(a,b) {
	if(a>b)
		return a;
	else
		return b;
}

/**
 * return the min of two values
 * @param a
 * @param b
 * @returns
 */
function getMin(a,b) {
	if(a>b)
		return b;
	else
		return a;
}

function manageHoverOnButton(id) {
	$($(id).find(".inactive")[0]).hover(function(event){
		
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});
	
}

/**
 * This function preload an image
 * @param img
 */
function preloadBackgroundImage(img) {
	var index1 = img.indexOf('http');
	var index2 = img.indexOf('png')+3;
	var image = new Image();
	image.src = img.substring(index1,index2);
	return image;
}